package junit.com.cpi.cornpp.languaje.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import com.cpi.cornpp.languaje.dao.LanguageDAO;
import com.cpi.cornpp.languaje.dto.LanguageDTO;
import com.cpi.cornpp.languaje.exception.LanguageException;
import com.cpi.cornpp.languaje.services.LanguageService;
import com.cpi.cornpp.languaje.view.LanguageView;
import com.cpi.cornpp.user.dto.UserDTO;
import com.cpi.cornpp.user.view.UserView;

public class LanguageDAOTestCase extends TestCase {


	LanguageDAO languageDAO = null;
	LanguageDTO languageDTO = null;
	LanguageView languageView = null;
	LanguageService languageService = null;
	UserDTO userDTO = null;
	UserView userView = null;
	List listLenguages = null;
	
	public LanguageDAOTestCase(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		this.languageDAO = new LanguageDAO();
		this.languageService = new LanguageService();
		this.languageDTO = new LanguageDTO();
		this.languageView = new LanguageView();
		this.userDTO = new UserDTO();
		this.userView = new UserView();
		this.listLenguages = new ArrayList(0);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	/**
	 * Test Case que obtiene 
	 * una lista de Lenguajes
	 */
	public void testGetListOfLanguages(){
	
		try {
			this.listLenguages = languageDAO.getListOfLanguajes();
			Iterator iterator = this.listLenguages.iterator();
			while(iterator.hasNext()){
				LanguageDTO e = (LanguageDTO)iterator.next();				
				System.out.println("ID : " + e.getId());
				System.out.println("Description : " + e.getDescription());
			}
			
			assertTrue(this.languageDAO.getListOfLanguajes().size() > 0);			
		} catch (Exception e) {
			fail(e.getMessage());
		}	
	}
	
	/**
	 * Test Case que inserta el Id del Lenguage
	 * y el Id del Usuario
	 */
	public void testInsertLanguageAndUser(){
		try {
			for( int int_i = 0; int_i < 1000; int_i++){
				languageDTO.setId("ES");
				userDTO.setUsrId("jrivas");
				languageDTO.setUserDTO(userDTO);
				assertTrue(this.languageDAO.insertLanguageUser(languageDTO));
			}
		} catch (Exception e) {
			fail(e.getMessage());
		}
			
	}
	
	public void testUpdateIfExistUserLanguage(){
		languageDTO.setId("EN");
		userDTO.setUsrId("jrivas");
		languageDTO.setUserDTO(userDTO);
		
		try {
			assertTrue(languageDAO.updateIfExistUserLanguage(languageDTO));
		} catch (LanguageException e) {
			e.printStackTrace();
		}
		
	}
	
	public void testLanguageService(){
		languageView.setId("ES");
		userView.setUsrId("kines");
		languageView.setUserView(userView);
				
		try {
			languageService.addUserLanguage(languageView);
		} catch (LanguageException e) {
			fail(e.getMessage());
		}
	}
}